//package com.apexsoft.assets.utils;
//
//import com.apex.ams.util.AasCommService;
//import com.apexsoft.aas.service.model.*;
//import com.apexsoft.assets.constants.Constants;
//import com.apexsoft.assets.constants.Messages;
//import com.apexsoft.assets.exception.BizException;
//import com.apexsoft.live.exception.AuthException;
//
//import org.apache.http.util.Asserts;
//import org.springframework.cglib.beans.BeanMap;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import java.util.HashMap;
//import java.util.Map;
//
//public class AmsUtil {
//
//    public static <T> AResponse<T> execute(String serviceName, String nameSpace, String pkg, Object request, Class<T> respVo, boolean accessControlled, String access) throws AuthException {
//        com.apexsoft.aas.service.model.AResponse result = getaResponse(serviceName, nameSpace, pkg, request, accessControlled, access);
//        return ResultUtil.result(result, respVo);
//    }
//
//    public static <T> com.apexsoft.aas.service.model.AResponse executeOrigin(String serviceName, String nameSpace, String pkg, Object request, Class<T> respVo, boolean accessControlled, String access) throws AuthException {
//        return getaResponse(serviceName, nameSpace, pkg, request, accessControlled, access);
//    }
//
//    private static com.apexsoft.aas.service.model.AResponse getaResponse(String serviceName, String nameSpace, String pkg, Object request, boolean accessControlled, String access) throws AuthException {
//        Asserts.notBlank(serviceName, "服务名不能为空");
//        Asserts.notBlank(nameSpace, "微服务命名空间不能为空");
//        Asserts.notBlank(pkg, "微服务包名不能为空");
//        if(accessControlled && !AccessPermissionUtil.hasAccess(access)){
//            throw new AuthException(-99, MessageUtils.get(Messages.ERROR_PERMISSION_DENIED));
//        }
//        ARequest aRequest = new ARequest();
//        if(request!=null){
//            Map resMap = new HashMap<>();
//            if(request instanceof Map){
//                resMap.putAll((Map) request);
//            }else {
//                resMap.putAll(BeanMap.create(request));
//            }
//            aRequest.setParams(resMap);
//        }
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        if(attributes!=null) {
//            String uuid = (String) attributes.getRequest().getAttribute("uuid");
//            aRequest.addParam("uuid", uuid);
//            if (UserSession.getUserSession() != null) {
//                aRequest.addParam(Constants.USERID, UserSession.getUserSession().getUserId());
//                aRequest.addParam(Constants.USERNAME, UserSession.getUserSession().getUserName());
//                aRequest.addHead(Constants.IP,getIp());
//            }
//        }
//        return AasCommService.sendRequest(nameSpace, pkg, serviceName, aRequest);
//    }
//
//    private static String getIp(){
//        String ip = UserSession.getRequestHolder().getHeader("x-real-ip");
//        if(ip == null || ip.length() == 0){
//            ip = UserSession.getRequestHolder().getRemoteAddr();
//        }
//        return ip;
//    }
//
//    public static ADownloadResponse download(String serviceName, String nameSpace, String pkg, Object request, boolean accessControlled, String access) throws AuthException {
//        Asserts.notBlank(serviceName, "服务名不能为空");
//        Asserts.notBlank(nameSpace, "微服务命名空间不能为空");
//        Asserts.notBlank(pkg, "微服务包名不能为空");
//        if(accessControlled && !AccessPermissionUtil.hasAccess(access)){
//            throw new AuthException(-99, MessageUtils.get(Messages.ERROR_PERMISSION_DENIED));
//        }
//        ADownloadRequest aRequest = new ADownloadRequest();
//        if(request!=null){
//            Map resMap = new HashMap<>();
//            resMap.putAll(BeanMap.create(request));
//            aRequest.setParams(resMap);
//        }
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        if(attributes!=null) {
//            String uuid = (String) attributes.getRequest().getAttribute("uuid");
//            aRequest.getParams().put("uuid", uuid);
//            if (UserSession.getUserSession() != null) {
//                aRequest.getParams().put(Constants.USERID, UserSession.getUserSession().getUserId());
//                aRequest.getParams().put(Constants.USERATTR, UserSession.getUserSession().getUser().getUserAttr());
//            }
//        }
//        ADownloadResponse aDownloadResponse = AasCommService.download(
//                nameSpace, pkg, serviceName, aRequest);
//        if(aDownloadResponse.getFileInfo().getCode()<0){
//            throw new BizException(aDownloadResponse.getFileInfo().getNote());
//        }
//        return aDownloadResponse;
//    }
//
//    public static AUploadResponse upload(String serviceName, String nameSpace, String pkg, AUploadRequest request, boolean accessControlled, String access) throws AuthException {
//        Asserts.notBlank(serviceName, "服务名不能为空");
//        Asserts.notBlank(nameSpace, "微服务命名空间不能为空");
//        Asserts.notBlank(pkg, "微服务包名不能为空");
//        if(accessControlled && !AccessPermissionUtil.hasAccess(access)){
//            throw new AuthException(-99, MessageUtils.get(Messages.ERROR_PERMISSION_DENIED));
//        }
//        UploadFileInfo uploadFileInfo = request.getFileInfo()!=null?request.getFileInfo():new UploadFileInfo();
//        if(uploadFileInfo.getParams()==null) {
//            uploadFileInfo.setParams(new HashMap<>());
//        }
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        if(attributes!=null) {
//            String uuid = (String) attributes.getRequest().getAttribute("uuid");
//            uploadFileInfo.getParams().put("uuid", uuid);
//            if (UserSession.getUserSession() != null) {
//                uploadFileInfo.getParams().put(Constants.USERID, UserSession.getUserSession().getUserId());
//                uploadFileInfo.getParams().put(Constants.USERATTR, UserSession.getUserSession().getUser().getUserAttr());
//            }
//        }
//        request.setFileInfo(uploadFileInfo);
//        AUploadResponse aUploadResponse = AasCommService.upload(
//                nameSpace, pkg, serviceName, request);
//        if(aUploadResponse.getCode()<0){
//            throw new BizException(aUploadResponse.getNote());
//        }
//        return aUploadResponse;
//    }
//
//}
